home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / UXENV.C < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  367 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/uxenv.c,v 1.7 1992/05/06 14:03:36 jinx Exp $
  4.  
  5. Copyright (c) 1990-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "ux.h"
  36. #include "osenv.h"
  37.  
  38. void
  39. DEFUN (OS_current_time, (buffer), struct time_structure * buffer)
  40. {
  41.   time_t t;
  42.   struct tm * ts;
  43.   STD_UINT_SYSTEM_CALL (syscall_time, t, (UX_time (0)));
  44.   STD_PTR_SYSTEM_CALL (syscall_localtime, ts, (UX_localtime (&t)));
  45.   (buffer -> year) = ((ts -> tm_year) + 1900);
  46.   (buffer -> month) = ((ts -> tm_mon) + 1);
  47.   (buffer -> day) = (ts -> tm_mday);
  48.   (buffer -> hour) = (ts -> tm_hour);
  49.   (buffer -> minute) = (ts -> tm_min);
  50.   (buffer -> second) = (ts -> tm_sec);
  51.   {
  52.     /* In localtime() encoding, 0 is Sunday; in ours, it's Monday. */
  53.     int wday = (ts -> tm_wday);
  54.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  55.   }
  56. }
  57.  
  58. #ifdef HAVE_TIMES
  59.  
  60. static clock_t initial_process_clock;
  61.  
  62. static void
  63. DEFUN_VOID (initialize_process_clock)
  64. {
  65.   struct tms buffer;
  66.   UX_times (&buffer);
  67.   initial_process_clock = (buffer . tms_utime);
  68. }
  69.  
  70. clock_t
  71. DEFUN_VOID (OS_process_clock)
  72. {
  73.   clock_t ct = (UX_SC_CLK_TCK ());
  74.   struct tms buffer;
  75.   /* Was STD_VOID_SYSTEM_CALL, but at least one version of Ultrix
  76.      returns negative numbers other than -1 when there are no errors.  */
  77.   while ((UX_times (&buffer)) == (-1))
  78.     if (errno != EINTR)
  79.       error_system_call (errno, syscall_times);
  80.   return
  81.     (((((buffer . tms_utime) - initial_process_clock) * 2000) + ct) /
  82.      (2 * ct));
  83. }
  84.  
  85. #else /* not HAVE_TIMES */
  86.  
  87. static void
  88. DEFUN_VOID (initialize_process_clock)
  89. {
  90. }
  91.  
  92. clock_t
  93. DEFUN_VOID (OS_process_clock)
  94. {
  95.   /* This must not signal an error in normal use. */
  96.   return (0);
  97. }
  98.  
  99. #endif /* HAVE_TIMES */
  100.  
  101. #ifdef HAVE_GETTIMEOFDAY
  102.  
  103. static struct timeval initial_rtc;
  104.  
  105. static void
  106. DEFUN_VOID (initialize_real_time_clock)
  107. {
  108.   struct timezone tz;
  109.   UX_gettimeofday ((&initial_rtc), (&tz));
  110. }
  111.  
  112. clock_t
  113. DEFUN_VOID (OS_real_time_clock)
  114. {
  115.   struct timeval rtc;
  116.   struct timezone tz;
  117.   STD_VOID_SYSTEM_CALL
  118.     (syscall_gettimeofday, (UX_gettimeofday ((&rtc), (&tz))));
  119.   return
  120.     ((((rtc . tv_sec) - (initial_rtc . tv_sec)) * 1000) +
  121.      ((((rtc . tv_usec) - (initial_rtc . tv_usec)) + 500) / 1000));
  122. }
  123.  
  124. #else /* not HAVE_GETTIMEOFDAY */
  125. #ifdef HAVE_TIMES
  126.  
  127. static clock_t initial_rtc;
  128.  
  129. static void
  130. DEFUN_VOID (initialize_real_time_clock)
  131. {
  132.   struct tms buffer;
  133.   initial_rtc = (UX_times (&buffer));
  134. }
  135.  
  136. clock_t
  137. DEFUN_VOID (OS_real_time_clock)
  138. {
  139.   clock_t ct = (UX_SC_CLK_TCK ());
  140.   struct tms buffer;
  141.   clock_t t;
  142.   /* Was STD_UINT_SYSTEM_CALL, but at least one version of Ultrix
  143.      returns negative numbers other than -1 when there are no errors.  */
  144.   while ((t = (UX_times (&buffer))) == (-1))
  145.     if (errno != EINTR)
  146.       error_system_call (errno, syscall_times);
  147.   return ((((t - initial_rtc) * 2000) + ct) / (2 * ct));
  148. }
  149.  
  150. #else /* not HAVE_TIMES */
  151.  
  152. static time_t initial_rtc;
  153.  
  154. static void
  155. DEFUN_VOID (initialize_real_time_clock)
  156. {
  157.   initial_rtc = (time (0));
  158. }
  159.  
  160. clock_t
  161. DEFUN_VOID (OS_real_time_clock)
  162. {
  163.   time_t t;
  164.   STD_UINT_SYSTEM_CALL (syscall_time, t, (UX_time (0)));
  165.   return ((t - initial_rtc) * 1000);
  166. }
  167.  
  168. #endif /* HAVE_TIMES */
  169. #endif /* HAVE_GETTIMEOFDAY */
  170.  
  171. #ifdef HAVE_ITIMER
  172.  
  173. static void
  174. DEFUN (set_timer, (which, first, interval),
  175.        int which AND
  176.        clock_t first AND
  177.        clock_t interval)
  178. {
  179.   struct itimerval value;
  180.   struct itimerval ovalue;
  181.   (value . it_value . tv_sec) = (first / 1000);
  182.   (value . it_value . tv_usec) = ((first % 1000) * 1000);
  183.   (value . it_interval . tv_sec) = (interval / 1000);
  184.   (value . it_interval . tv_usec) = ((interval % 1000) * 1000);
  185.   STD_VOID_SYSTEM_CALL
  186.     (syscall_setitimer, (UX_setitimer (which, (&value), (&ovalue))));
  187. }
  188.  
  189. void
  190. DEFUN (OS_process_timer_set, (first, interval),
  191.        clock_t first AND
  192.        clock_t interval)
  193. {
  194.   set_timer (ITIMER_VIRTUAL, first, interval);
  195. }
  196.  
  197. void
  198. DEFUN_VOID (OS_process_timer_clear)
  199. {
  200.   set_timer (ITIMER_VIRTUAL, 0, 0);
  201. }
  202.  
  203. void
  204. DEFUN (OS_real_timer_set, (first, interval),
  205.        clock_t first AND
  206.        clock_t interval)
  207. {
  208.   set_timer (ITIMER_REAL, first, interval);
  209. }
  210.  
  211. void
  212. DEFUN_VOID (OS_real_timer_clear)
  213. {
  214.   set_timer (ITIMER_REAL, 0, 0);
  215. }
  216.  
  217. #else /* not HAVE_ITIMER */
  218.  
  219. static unsigned int alarm_interval;
  220.  
  221. void
  222. DEFUN_VOID (reschedule_alarm)
  223. {
  224.   UX_alarm (alarm_interval);
  225. }
  226.  
  227. void
  228. DEFUN (OS_process_timer_set, (first, interval),
  229.        clock_t first AND
  230.        clock_t interval)
  231. {
  232.   error_unimplemented_primitive ();
  233. }
  234.  
  235. void
  236. DEFUN_VOID (OS_process_timer_clear)
  237. {
  238. }
  239.  
  240. void
  241. DEFUN (OS_real_timer_set, (first, interval),
  242.        clock_t first AND
  243.        clock_t interval)
  244. {
  245.   alarm_interval = ((interval + 999) / 1000);
  246.   UX_alarm ((first + 999) / 1000);
  247. }
  248.  
  249. void
  250. DEFUN_VOID (OS_real_timer_clear)
  251. {
  252.   alarm_interval = 0;
  253.   UX_alarm (0);
  254. }
  255.  
  256. #endif /* HAVE_ITIMER */
  257.  
  258. void
  259. DEFUN_VOID (UX_initialize_environment)
  260. {
  261.   initialize_process_clock ();
  262.   initialize_real_time_clock ();
  263. #ifndef HAVE_ITIMER
  264.   alarm_interval = 0;
  265. #endif
  266. }
  267.  
  268. static size_t current_dir_path_size = 0;
  269. static char * current_dir_path = 0;
  270.  
  271. CONST char *
  272. DEFUN_VOID (OS_working_dir_pathname)
  273. {
  274.   if (current_dir_path) {
  275.     return (current_dir_path);
  276.   }
  277.   if (current_dir_path_size == 0)
  278.     {
  279.       current_dir_path = (UX_malloc (1024));
  280.       if (current_dir_path == 0)
  281.     error_system_call (ENOMEM, syscall_malloc);
  282.       current_dir_path_size = 1024;
  283.     }
  284.   while (1)
  285.     {
  286.       if ((UX_getcwd (current_dir_path, current_dir_path_size)) != 0)
  287.     return (current_dir_path);
  288.       if (errno != ERANGE)
  289.     error_system_call (errno, syscall_getcwd);
  290.       current_dir_path_size *= 2;
  291.       {
  292.     char * new_current_dir_path =
  293.       (UX_realloc (current_dir_path, current_dir_path_size));
  294.     if (new_current_dir_path == 0)
  295.       /* ANSI C requires `path' to be unchanged -- we may have to
  296.          discard it for systems that don't behave thus. */
  297.       error_system_call (ENOMEM, syscall_realloc);
  298.     current_dir_path = new_current_dir_path;
  299.       }
  300.     }
  301. }
  302.  
  303. void
  304. DEFUN (OS_set_working_dir_pathname, (name), CONST char * name)
  305. {
  306.   size_t name_size = strlen (name);
  307.   STD_VOID_SYSTEM_CALL (syscall_chdir, (UX_chdir (name)));
  308.   while (1) {
  309.     if (name_size < current_dir_path_size) {
  310.       strcpy(current_dir_path, name);
  311.       return;
  312.     } 
  313.     current_dir_path_size *= 2;
  314.     {
  315.       char * new_current_dir_path =
  316.     (UX_realloc (current_dir_path, current_dir_path_size));
  317.       if (new_current_dir_path == 0)
  318.     error_system_call (ENOMEM, syscall_realloc);
  319.       current_dir_path = new_current_dir_path;
  320.     }
  321.   }
  322. }
  323.  
  324. CONST char *
  325. DEFUN (OS_get_environment_variable, (name), CONST char * name)
  326. {
  327.   return (UX_getenv (name));
  328. }
  329.  
  330. CONST char *
  331. DEFUN_VOID (OS_current_user_name)
  332. {
  333.   {
  334.     CONST char * result = (UX_getlogin ());
  335.     if ((result != 0) && (*result != '\0'))
  336.       return (result);
  337.   }
  338.   {
  339.     struct passwd * entry = (UX_getpwuid (UX_geteuid ()));
  340.     if (entry != 0)
  341.       return (entry -> pw_name);
  342.   }
  343.   error_external_return ();
  344.   return (0);
  345. }
  346.  
  347. CONST char *
  348. DEFUN_VOID (OS_current_user_home_directory)
  349. {
  350.   {
  351.     char * user_name = (UX_getlogin ());
  352.     if (user_name != 0)
  353.       {
  354.     struct passwd * entry = (UX_getpwnam (user_name));
  355.     if (entry != 0)
  356.       return (entry -> pw_dir);
  357.       }
  358.   }
  359.   {
  360.     struct passwd * entry = (UX_getpwuid (UX_geteuid ()));
  361.     if (entry != 0)
  362.       return (entry -> pw_dir);
  363.   }
  364.   error_external_return ();
  365.   return (0);
  366. }
  367.